<template>
  <j-modal
    title="汽车进项发票登记(为必填项)"
    :width="500"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="车辆开票时间">
          <a-input v-model="form.time" />
        </a-form-model-item>
        <a-form-model-item label="购货单位名称">
          <a-input v-model="form.Unitname" />
        </a-form-model-item>
        <a-form-model-item label="车辆发票单位">
          <a-select v-model="form.carUnit" placeholder="请选择">
            <a-select-option value="shanghai">
              上海腾讯分公司
            </a-select-option>
            <a-select-option value="beijing">
              阿里巴巴
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="车辆数(辆)">
          <a-input v-model="form.carnum" />
        </a-form-model-item>
        <a-form-model-item label="进项发票类型">
          <a-select v-model="form.region" placeholder="请选择">
            <a-select-option value="shanghai">
              机动车销售统一发票
            </a-select-option>
            <a-select-option value="beijing">
              增值税专用发票
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="进项发票代码  ">
          <a-input v-model="form.InvoiceCode" />
        </a-form-model-item>
        <a-form-model-item label="进项发票编号  ">
          <a-input v-model="form.Invoicenum" />
        </a-form-model-item>
        <a-form-model-item label="价格合计(元)  ">
          <a-input v-model="form.money" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div style="display: flex">
      <div style="width: 120px">备注：</div>
      <a-textarea  style="width: 275px" :rows="4"/>
    </div>
    <!--<a-button type="primary" @click="saveDraft">暂存</a-button>-->
  </j-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'

  export default {
    name: 'InputRegisterModel',
    data() {
      return {
        title: '交易信息',
        visible: false,
        model: {},
        saleSource: [],
        SaleInfosLike: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: '/cxm/cxmOrderManager/add',
          edit: '/cxm/cxmOrderManager/edit',
          saveDraft: 'cxm/cxmCommonBusiness/saveDraft',
          getDraft: 'cxm/cxmCommonBusiness/getDraft',
          getSaleInfosLike: 'cxm/cxmSalesInfo/getSaleInfosLike',
          getSaleInfos: '/cxm/cxmOrderManager/getSaleInfos'
        },
        showDing: false,
        showEnterpriseUscc: false
      }
    },
    created() {
    },
    methods: {
      saleSelect(value) {
        console.log('saleSelect', value)
        for (let i = 0; i < this.SaleInfosLike.length; i++) {
          if (this.SaleInfosLike[i].salesName == value) {
            this.form.setFieldsValue({
              salesPhone: this.SaleInfosLike[i].salesPhone
            })
          }
        }
      },
      add() {
        const that = this
        let httpurl = this.url.getDraft
        let params = {
          bType: 'order'
        }
        getAction(httpurl, params).then((res) => {
          if (res.success) {
            if (res.result) {
              that.edit(JSON.parse(res.result))
            } else {
              getAction(that.url.getSaleInfos, params).then((res) => {
                that.edit(res.result)
              })
            }
          } else {
            that.edit({})
          }
        })
      },
      edit(record) {
        console.log(record)
        this.form.resetFields()
        this.model = Object.assign({}, record)
        console.log(this.model)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue({
            batteryNumber: this.model.batteryNumber,
            businessAddress: this.model.businessAddress,
            carBrand: this.model.carBrand,
            carColor: this.model.carColor,
            carModel: this.model.carModel,
            carType: this.model.carModel,
            carTypeConfiguration: this.model.carTypeConfiguration,
            carVin: this.model.carVin,
            depositAttribute: this.model.depositAttribute,
            depositContent: this.model.depositContent,
            documentNumber: this.model.documentNumber,
            documentType: this.model.documentType,
            engineNumber: this.model.engineNumber,
            enterpriseId: this.model.enterpriseId,
            enterpriseName: this.model.enterpriseName,
            enterpriseUscc: this.model.enterpriseUscc,
            epositAmount: this.model.epositAmount,
            fuelType: this.model.fuelType,
            importEngineNumber: this.model.importEngineNumber,
            importProveNumber: this.model.importProveNumber,
            interiorColor: this.model.interiorColor,
            isNewCar: this.model.isNewCar,
            orderDate: this.model.orderDate ? moment(this.model.orderDate) : null,
            orderNumber: this.model.orderNumber,
            operationAddr: this.model.operationAddr,
            ownerName: this.model.ownerName,
            ownerPhone: this.model.ownerPhone,
            qualifiedNumber: this.model.qualifiedNumber,
            quoteAmount: this.model.quoteAmount,
            registerDistinct: this.model.registerDistinct,
            salesName: this.model.salesName,
            salesPhone: this.model.salesPhone
          })
        })
        if (this.model.enterpriseUscc) {
          this.showEnterpriseUscc = true
        } else {
          this.showEnterpriseUscc = false
        }

      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {

          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add

              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            //时间格式化
            formData.orderDate = formData.orderDate ? formData.orderDate.format('YYYY-MM-DD HH:mm:ss') : null

            httpAction(httpurl, formData, method)
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.confirmLoading = false
                that.close()
              })
          }
        })
      },
      saveDraft() {
        const that = this
        this.form.validateFields((err, values) => {
          let formData = Object.assign(this.model, values)
          //时间格式化
          formData.orderDate = formData.orderDate ? formData.orderDate.format('YYYY-MM-DD HH:mm:ss') : null
          let httpurl = this.url.saveDraft

          console.log(JSON.stringify(formData))
          let Data = {}
          Data.btype = 'order'
          Data.content = JSON.stringify(formData)
          console.log(Data)
          httpAction(httpurl, Data, 'post')
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        })

      },
      handleCancel() {
        this.close()
      }
    }
  }
</script>

<style lang="less" scoped>
</style>